import {
  Answer,
  Assigner,
  End,
  Home,
  Http,
  IfElse,
  Iteration,
  ParameterExtractor,
  QuestionClassifier,
  TemplatingTransform,
  VariableX
} from '@/components/workflow/base/icons/src/vender/workflow'
import type { FC } from 'react'
import { memo } from 'react'
import { RiHome5Fill } from '@remixicon/react'
import { WorkflowHoveredIcon } from '../bs-icons/workflow'
import { codeSvg, intendSvg, knowledgeSvg, llmSvg, pluginSvg,extractorSvg, textConcatSvg,varUpdateSvg,listOperationSvg, loopSvg } from './icons'
import { BlockEnum } from './types'

type BlockIconProps = {
  type: BlockEnum
  size?: string
  className?: string
  toolIcon?: string | { content: string; background: string }
}
const ICON_CONTAINER_CLASSNAME_SIZE_MAP: Record<string, string> = {
  xs: 'w-4 h-4 rounded-[5px] shadow-xs',
  sm: 'w-5 h-5 rounded-md shadow-xs',
  md: 'w-6 h-6 rounded-lg shadow-md'
}

const getIcon = (type: BlockEnum, className: string) => {
  return {
    [BlockEnum.Start]: <Home className={className} />,
    [BlockEnum.LLM]: llmSvg,
    [BlockEnum.Code]: codeSvg,
    [BlockEnum.End]: <End className="h-3.5 w-3.5 text-[#ffffff]" />,
    [BlockEnum.IfElse]: <IfElse className="h-3.5 w-3.5 text-[#ffffff]" />,
    [BlockEnum.HttpRequest]: <Http className='h-3.5 w-3.5 text-[#ffffff]' />,
    [BlockEnum.Answer]: <Answer className={className} />,
    [BlockEnum.KnowledgeRetrieval]: knowledgeSvg,
    [BlockEnum.QuestionClassifier]: <QuestionClassifier className={className} />,
    [BlockEnum.TemplateTransform]: <TemplatingTransform className={className} />,
    [BlockEnum.VariableAssigner]: <VariableX className={className} />,
    [BlockEnum.VariableAggregator]: <VariableX className={className} />,
    [BlockEnum.Assigner]: <Assigner className={className} />,
    [BlockEnum.Tool]: pluginSvg,
    [BlockEnum.Iteration]: <Iteration className="h-3.5 w-3.5 text-[#ffffff]" />,
    [BlockEnum.ParameterExtractor]: <ParameterExtractor className={className} />,
    [BlockEnum.Intend]: intendSvg,
    [BlockEnum.IterationStart]: <RiHome5Fill className='h-3.5 w-3.5 text-[#ffffff]'/>,
    [BlockEnum.IterationEnd]: <End className="h-3.5 w-3.5 text-[#ffffff]" />,
    [BlockEnum.TextConcatenation]:textConcatSvg,
    [BlockEnum.Workflow]: <WorkflowHoveredIcon className="my-[12px] h-6 w-6 rounded-lg text-[#ffffff] " />,
    [BlockEnum.VarUpdate]: varUpdateSvg,
    [BlockEnum.DocExtractor]: extractorSvg,
    [BlockEnum.List]: listOperationSvg,
    [BlockEnum.Loop]: loopSvg,
    [BlockEnum.LoopStart]: <RiHome5Fill className='h-3.5 w-3.5 text-[#ffffff]'/>,
    [BlockEnum.LoopEnd]: <End className="h-3.5 w-3.5 text-[#ffffff]" />,
  }[type]
}
const ICON_CONTAINER_BG_COLOR_MAP: Record<string, string> = {
  [BlockEnum.Start]: 'bg-primary-500',
  [BlockEnum.LLM]: 'bg-[#6172f3]',
  [BlockEnum.Code]: 'bg-[#2e90fa]',
  [BlockEnum.End]: 'bg-[#F79009]',
  [BlockEnum.IfElse]: 'bg-[#06aed4]',
  [BlockEnum.Iteration]: 'bg-[#06AED4]',
  [BlockEnum.HttpRequest]: 'bg-[#875BF7]',
  [BlockEnum.Answer]: 'bg-[#F79009]',
  [BlockEnum.KnowledgeRetrieval]: 'bg-[#17b26a]',
  [BlockEnum.QuestionClassifier]: 'bg-[#16B364]',
  [BlockEnum.TemplateTransform]: 'bg-[#2E90FA]',
  [BlockEnum.VariableAssigner]: 'bg-[#2E90FA]',
  [BlockEnum.VariableAggregator]: 'bg-[#2E90FA]',
  [BlockEnum.Assigner]: 'bg-[#2E90FA]',
  [BlockEnum.ParameterExtractor]: 'bg-[#2E90FA]',
  [BlockEnum.Tool]: 'bg-[#f2b600]',
  [BlockEnum.Intend]: 'bg-[#ff811a]',
  [BlockEnum.DocExtractor]: 'bg-[#ff811a]',
  [BlockEnum.Workflow]: 'bg-[#3ec254]',
  [BlockEnum.TextConcatenation]: 'bg-[#ff811a]',
  [BlockEnum.VarUpdate]: 'bg-[#2E90FA]',  // 添加变量更新背景色
  [BlockEnum.IterationStart]: 'bg-[#06AED4]', 
  [BlockEnum.IterationEnd]: 'bg-[#06AED4]',
  [BlockEnum.List]: 'bg-[#9E77ED]',
  [BlockEnum.Loop]: 'bg-[#C11574]',
  [BlockEnum.LoopStart]: 'bg-[#C11574]', 
  [BlockEnum.LoopEnd]: 'bg-[#C11574]',
}

const getSize = size => {
  switch (size) {
    case 'xs':
      return 'w-3 h-3'
    case 'sm':
      return 'w-3.5 h-3.5'
    case 'md':
      return 'w-4 h-4'
    default:
      return 'w-3.5 h-3.5'
  }
}

const BlockIcon: FC<BlockIconProps> = ({ type, size = 'sm', className }) => {
  return (
    <div
      className={`
      border-white/2 flex items-center justify-center text-white
      ${ICON_CONTAINER_CLASSNAME_SIZE_MAP[size]}
      ${ICON_CONTAINER_BG_COLOR_MAP[type]}
      ${className}
    `}
    >
      {getIcon(type, getSize(size))}
    </div>
  )
}

export const VarBlockIcon: FC<BlockIconProps> = ({ type, className }) => {
  return <>{getIcon(type, `w-3 h-3 ${className}`)}</>
}

export default memo(BlockIcon)
